package com.yupi.springbootinit.datasource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.model.dto.post.PostQueryRequest;
import com.yupi.springbootinit.model.entity.Post;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.service.PostService;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class PostDatasource implements SearchDatasource {

    @Resource
    private PostService postService;
    @Override
    public Page<PostVO> search(String searchText, int pageNum, int pageSize) {
        PostQueryRequest postQueryRequest=new PostQueryRequest();
        postQueryRequest.setSearchText(searchText);
//        Page<Post> postPage = postService.page(new Page<>(pageNum, pageSize),
//                postService.getQueryWrapper(postQueryRequest));
        Page<Post> postPage = postService.searchFromEs(postQueryRequest);
                ServletRequestAttributes requestAttributes = ServletRequestAttributes.class.
                cast(RequestContextHolder.getRequestAttributes());
        HttpServletRequest contextRequest = requestAttributes.getRequest();
        Page<PostVO> postVOPage = postService.getPostVOPage(postPage, contextRequest);
        return postVOPage;
    }
}
